// DbTableShow.java

import java.io.*;
import java.sql.*;

public class DbTableShow
{
	public static void main( String[] argv )
	{
		String sDbDrv=null, sDbUrl=null, sTable=null, sUsr="", sPwd="";
		if( 3 <= argv.length ) {
			sDbDrv = argv[0];
			sDbUrl = argv[1];
			sTable = argv[2];
			if( 4 <= argv.length )  sUsr = argv[3];
			if( 5 <= argv.length )  sPwd = argv[4];
		} else {

			BufferedReader in = new BufferedReader(
					new InputStreamReader( System.in ) );
			System.out.println( "Name des Datenbanktreibers eingeben (z.B. com.mysql.jdbc.Driver):" );
			//sDbDrv = in.readLine();
			sDbDrv = "com.mysql.jdbc.Driver";
			System.out.println( "Url der Datenbank eingeben (z.B. jdbc:mysql://localhost:3306/MeineDb):" );
			//sDbUrl = in.readLine();
			sDbUrl = "jdbc:mysql://db.hosting-agency.de/db0095122";
			System.out.println( "Name der Tabelle eingeben (z.B. MeineTestTabelle):" );
			//sTable = in.readLine();
			sTable = "nmea";
			System.out.println( "Benutzername (z.B. root):" );
			//sUsr = in.readLine();
			sUsr = "dbuser19577";
			System.out.println( "Passwort (z.B. mysqlpwd):" );
			//sPwd = in.readLine();
			sPwd = "1588.nc";
		}
		if( null != sDbDrv && 0 < sDbDrv.length() &&
				null != sDbUrl && 0 < sDbUrl.length() &&
				null != sTable && 0 < sTable.length() ) {
			Connection cn = null;
			Statement  st = null;
			ResultSet  rs = null;
			try {
				// Select fitting database driver and connect:
				Class.forName( sDbDrv );
				cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
				st = cn.createStatement();
				rs = st.executeQuery( "select * from " + sTable );
				// Get meta data:
				ResultSetMetaData rsmd = rs.getMetaData();
				int i, n = rsmd.getColumnCount();
				// Print table content:
				for( i=0; i<n; i++ )
					System.out.print( "+---------------" );
				System.out.println( "+" );
				for( i=1; i<=n; i++ )    // Attention: first column with 1 instead of 0
					System.out.print( "| " + extendStringTo14( rsmd.getColumnName( i ) ) );
				System.out.println( "|" );
				for( i=0; i<n; i++ )
					System.out.print( "+---------------" );
				System.out.println( "+" );
				while( rs.next() ) {
					for( i=1; i<=n; i++ )  // Attention: first column with 1 instead of 0
						System.out.print( "| " + extendStringTo14( rs.getString( i ) ) );
					System.out.println( "|" );
				}
				for( i=0; i<n; i++ )
					System.out.print( "+---------------" );
				System.out.println( "+" );
			} catch( Exception ex ) {
				System.out.println( ex );
			} finally {
				try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
				try { if( null != st ) st.close(); } catch( Exception ex ) {}
				try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
			}
		}
	}

	// Extend String to length of 14 characters
	private static final String extendStringTo14( String s )
	{
		if( null == s ) s = "";
		final String sFillStrWithWantLen = "              ";
		final int iWantLen = sFillStrWithWantLen.length();
		final int iActLen  = s.length();
		if( iActLen < iWantLen )
			return (s + sFillStrWithWantLen).substring( 0, iWantLen );
		if( iActLen > 2 * iWantLen )
			return s.substring( 0, 2 * iWantLen );
		return s;
	}
}

